from enum import Enum
from typing import Dict, Any, List, Optional
from pydantic import BaseModel

# 房间状态
class RoomState(Enum):
    WAITING_FOR_PLAYERS = "waiting_for_players"
    READY_TO_START = "ready_to_start"
    GAME_IN_PROGRESS = "game_in_progress"
    GAME_FINISHED = "game_finished"

# 游戏状态
class GameState(Enum):
    DEALING_CARDS = "dealing_cards"
    POSTING_BLINDS = "posting_blinds"
    PREFLOP_BETTING = "preflop_betting"
    DEALING_FLOP = "dealing_flop"
    FLOP_BETTING = "flop_betting"
    DEALING_TURN = "dealing_turn"
    TURN_BETTING = "turn_betting"
    DEALING_RIVER = "dealing_river"
    RIVER_BETTING = "river_betting"
    SHOWDOWN = "showdown"
    ROUND_END = "round_end"

# 下注状态
class BettingState(Enum):
    WAITING_FOR_ACTION = "waiting_for_action"
    PROCESSING_ACTION = "processing_action"
    BETTING_COMPLETE = "betting_complete"

# 玩家行动事件
class PlayerAction(Enum):
    CHECK = "check"
    CALL = "call"
    BET = "bet"
    RAISE = "raise"
    FOLD = "fold"
    ALL_IN = "all_in"

# 客户端到服务器的消息类型
class ClientMessageType(Enum):
    JOIN_ROOM = "join_room"
    PLAYER_ACTION = "player_action"
    START_GAME = "start_game"
    LEAVE_ROOM = "leave_room"

# 服务器到客户端的消息类型
class ServerMessageType(Enum):
    ROOM_STATE = "room_state"
    GAME_STATE = "game_state"
    ACTION_REQUIRED = "action_required"
    PLAYER_JOINED = "player_joined"
    PLAYER_LEFT = "player_left"
    GAME_STARTED = "game_started"
    SHOWDOWN_RESULT = "showdown_result"  # 新增：摊牌结果
    AI_ACTION = "ai_action"  # 新增：AI行动通知
    ROUND_END = "round_end"  # 新增：回合结束
    ERROR = "error"

# 消息数据模型
class JoinRoomData(BaseModel):
    player_name: str
    total_players: int = 6  # 房间总人数
    ai_players: int = 5     # AI玩家数量

class PlayerActionData(BaseModel):
    action: PlayerAction
    amount: Optional[int] = None

class GameStateData(BaseModel):
    community_cards: List[Dict[str, str]]  # [{"rank": "A", "suit": "♠"}, ...]
    pot: int
    current_bet: int
    players: List[Dict[str, Any]]
    your_hand: Optional[List[Dict[str, str]]]
    your_turn: bool
    valid_actions: List[PlayerAction]
    to_call: int
    min_raise: Optional[int]

class RoomStateData(BaseModel):
    room_state: RoomState
    players_count: int
    total_players: int
    ai_players: int
    players: List[Dict[str, Any]]

class ActionRequiredData(BaseModel):
    player_name: str
    timeout: int
    valid_actions: List[PlayerAction]
    to_call: int
    min_raise: Optional[int]
    pot: int
    current_bet: int

# 通用消息格式
class WebSocketMessage(BaseModel):
    type: str
    data: Dict[str, Any]

class ErrorData(BaseModel):
    message: str
    code: Optional[str] = None